/**
 * 
 */
package net.litxuf.Util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

import org.apache.commons.fileupload.*;
import com.yeqiangwei.image.ImageFactory;
import com.yeqiangwei.image.ImageOperate;
import com.yeqiangwei.util.StringHelper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 根据id上传图片
 * @author XuFeng
 *
 *
 */
public class UploadFileServlet {	
	static int gene=1000;	
	static String imgSavePath = "";
	static {
		try {
			Properties properties = new Properties();
			InputStream ins = UploadFileServlet.class.getResourceAsStream("/siteConfig.properties");
			properties.load(ins);
			imgSavePath = (String) properties.get("imgSavePath");
			 
		} catch (IOException e) {
			
				e.printStackTrace();
			
		}
	}
	/***
	 * 
	 * Made in 2007-10-2上午02:43:12
	 *  
	 *@param id 公园的ID
	 *@param typeName// 文件夹的名称 
	 *@param videoID// 视频的ID 图片的名称为视频id.jpg或 id.gif 
	 *@param request
	 *@return
	 *@throws Exception
	 *
	 */
 
public static boolean loadfile(int fatherID,String typeName,int videoID, HttpServletRequest request,HttpServletResponse response) throws Exception{
		 
		String path=getFileAngPath(fatherID,typeName,videoID);
		DiskFileUpload dfu = new DiskFileUpload();
		dfu.setHeaderEncoding("utf-8");
		List<FileItem> list = (List<FileItem>)dfu.parseRequest(request); 
		response.setContentType("text/html; charset=utf-8");
		response.setHeader("Cache-Control","no-cache");
		String  fileName=null;
		for (FileItem item : list) {
			if (!item.isFormField())
				fileName= item.getName();
			if (RegUtil.isImage(fileName)) {
				
					File file = new File(path);
						File dir = file.getParentFile();
					if (!dir.exists())	
					{
						dir.mkdirs();	
					}
					item.write(file);		
					
					String fileExt=getFileExt(fileName);
				
					ImageOperate image = null;
					if (fileExt.equals("gif")) {
						image = ImageFactory.creator(ImageOperate.GIF);
					} else {
					image = ImageFactory.creator(ImageOperate.JPEG);
					}
					
				 image.narrowOrEnlarge(getFileAngPath(fatherID,typeName,videoID), getFileAngPath(fatherID,typeName,videoID), 100, 100);
					
				return true;
				}
			}
		
		return false;
	}

private static String getPath(int uid,String delimiter)
    {
        StringBuffer path=new StringBuffer("");
            while((uid/gene)>0)
            {
                path.append("_"+uid%gene+delimiter);
                uid=(uid/gene);
            }
            path.append(uid);
            return path.toString();
    }
 
	public  static String getPathMD5(int id){
    	String s = String.valueOf(id);		 
		s = StringHelper.encodeString("md5of32", s);		 
		s = s.substring(0, 2).toLowerCase();
		return   id+"_"+s+"/";
    }
    /**
     * 
     * Made in 2007-9-29上午02:31:45
     *  
     *@param id
     *@param typeName 
     *@return
     *
     */
	private static String getFileAngPath(int fatherID,String typeName,int videoID){
    	return imgSavePath+typeName+"/"+getPathMD5(fatherID)+videoID;
    	
    	
    }
    public static void main(String ar[]){    	
    	System.out.println(getFileAngPath(3,"video",9));
    	System.out.println(getPathMD5(3));
 
    }   
    
    private static String getFileExt(String fileName){
    	String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
    	return fileExt;
    	
    }
}
